package com.sgb.kjwl.view.ui.fragment.manage

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.scwang.smart.refresh.layout.listener.ScrollBoundaryDecider
import com.sgb.kjwl.AppLazyFragment
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.FragmentOtherInquiryBinding
import com.sgb.kjwl.di.self.DaggerSelfFragComponent
import com.sgb.kjwl.di.self.SelfFragModule
import com.sgb.kjwl.viewmodel.manage.OtherInquiryModel
import com.swgk.core.base.di.AppComponent
import javax.inject.Inject

class OtherInquiryFragment : AppLazyFragment() {

    private var mType : Int ?= null
    private lateinit var mBinding : FragmentOtherInquiryBinding
    private var isLoadMore = false // 是否是加载更多

    @Inject
    lateinit var mViewModel : OtherInquiryModel

    private lateinit var mAdapter: WebSiteInquiryFragment.WebSiteInquiryAdapter

    private var mEmptyTip : String ?= null //缺省提示语

    companion object{
        const val TAG = "otherInquiryFragment"
        const val KEY_TYPE = "key_type"
        const val KEY_TITLE = "key_title"
        const val TYPE_ZHAO_CAI = 1080 //招采
        const val TYPE_JI_CAI = 1081 //集采
        const val TYPE_GOODS = 1082 //商品
        const val TYPE_LEASE = 1083 //租赁
        const val TYPE_WORK_TEAM = 1084 //工队
        const val TYPE_SERVICE = 1085 //服务
            fun newInstance(type : Int)  : OtherInquiryFragment{
                val fragment = OtherInquiryFragment()
                val bundle = Bundle()
                bundle.putInt(KEY_TYPE, type)
                fragment.arguments = bundle
                return fragment
            }
        fun newInstance() : OtherInquiryFragment{
            val fragment = OtherInquiryFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

        fun newInstance(emptyTip : String) : OtherInquiryFragment{
            val fragment = OtherInquiryFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            bundle.putString(KEY_TITLE, emptyTip)
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null){
            mType = requireArguments().getInt(KEY_TYPE)
            mEmptyTip = requireArguments().getString(KEY_TITLE)
        }
    }

    override fun setupFragmentComponent(appComponent: AppComponent?) {
        DaggerSelfFragComponent.builder().appComponent(appComponent).
        selfFragModule(SelfFragModule()).build().inject(this)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_other_inquiry, container, false)
        return mBinding.root
    }

    override fun onLazyLoad() {

    }

    override fun initView(){
        mBinding.recycleView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        // SwipeRefreshLayout 刷新
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                isLoadMore = true
                mBinding.refreshLayout.finishLoadMore(500)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                isLoadMore = false
                mBinding.refreshLayout.finishRefresh(500)
            }
        })
        //解决 在实现 上拉加载更多 和 下拉刷新 时  遇到item不在顶端就执行了刷新的方法和数据item没有填满屏幕也执行了加载更多
        mBinding.refreshLayout.setScrollBoundaryDecider(object : ScrollBoundaryDecider {
            override fun canRefresh(content: View): Boolean {
                if (mBinding.recycleView == null) return false
                return mBinding.recycleView.computeVerticalScrollOffset() === 0
            }

            override fun canLoadMore(content: View): Boolean {
                if (mBinding.recycleView == null) return false
                //整个View控件的高度
                val scrollRange: Int = mBinding.recycleView.computeVerticalScrollRange()
                //当前屏幕之前滑过的距离
                val scrollOffset: Int = mBinding.recycleView.computeVerticalScrollOffset()
                //当前屏幕显示的区域高度
                val scrollExtent: Int = mBinding.recycleView.computeVerticalScrollExtent()
                //获取recyclerView的高度
                val height: Int = mBinding.recycleView.height
                if (height > scrollRange) {
                    return false
                }
                return scrollRange <= scrollOffset + scrollExtent
            }
        })
        //暂时不知道ui 所以先用网站函询的itemUI代替
        mAdapter = WebSiteInquiryFragment.WebSiteInquiryAdapter(R.layout.item_website_inquiry)
        mAdapter.bindToRecyclerView(mBinding.recycleView)
        val empty = LayoutInflater.from(context).inflate(R.layout.layout_empty_view, mBinding.recycleView, false)
        if (!TextUtils.isEmpty(mEmptyTip)){
            empty.findViewById<TextView>(R.id.no_data_tv).text = mEmptyTip
        }
        mAdapter.emptyView = empty
    }

}